package com.yaya.spring.mybatis.config;

import com.yaya.spring.mybatis.ZyyScan;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


/**
 * MybatisConfig
 *
 * @author zhougf
 * @date 2019/9/24
 */
@Configuration
@ComponentScan("com.yaya.spring.mybatis")
@ZyyScan
public class MybatisConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean.getObject();
    }

    @Bean()
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManager = new DriverManagerDataSource();
        driverManager.setDriverClassName("com.mysql.jdbc.Driver");
        driverManager.setUsername("root");
        driverManager.setPassword("$@390541asdDSA");
        driverManager.setUrl("jdbc:mysql://49.234.184.233:3306/washcar?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
        return driverManager;
    }
//    @Bean
//    public UserMapper userMapper(SqlSessionFactory sqlSessionFactory) throws Exception {
//        MapperFactoryBean mapperFactoryBean = new MapperFactoryBean();
//        mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);
//        mapperFactoryBean.setMapperInterface(UserMapper.class);
//        return (UserMapper)mapperFactoryBean.getObject();
//    }

}
